package com.finger.eduservice.controller;


import com.finger.commonutils.R;
import com.finger.eduservice.entity.EduChapter;
import com.finger.eduservice.entity.vo.ChapterVO;
import com.finger.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-06
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 根据课程ID返回所有的章节小节
     * @param courseId
     * @return
     */
    @GetMapping("/getAllChapter/{courseId}")
    public R getAllChapter(@PathVariable String courseId){
        List<ChapterVO> list = eduChapterService.getAllChapter(courseId);
        return R.ok().data("list",list);
    }

    /**
     * 添加章节
     * @param eduChapter 章节对象
     * @return
     */
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        return R.ok().data("SUCCESS",eduChapterService.save(eduChapter));
    }

    /**
     * 根据章节ID返回章节
     * @param chapterId 章节ID
     * @return
     */
    @GetMapping("/getChapter/{chapterId}")
    public R getChapter(@PathVariable String chapterId){
        return R.ok().data("chapter",eduChapterService.getById(chapterId));
    }

    /**
     * 根据章节ID删除章节
     * @param chapterId
     * @return
     */
    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        return eduChapterService.removeChapterById(chapterId);
    }

    /**
     * 根据章节ID更新章节
     * @param eduChapter
     * @return
     */
    @PutMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }


}

